<?php

namespace app\controller;

use app\model\Card;
use app\model\ExploreAddress;
use app\model\ExploreChapter;
use app\model\ExploreDialogue;
use app\model\ExploreNavigation;
use app\model\ExploreStory;
use app\model\ExploreTask;
use app\model\Fish;
use app\model\GameConfig;
use app\model\Material;
use app\model\Staff;
use app\model\User;
use app\model\UserBackpack;
use app\model\UserEmail;
use app\model\UserFactory;
use app\model\UserFactorySeat;
use app\model\UserFinancialRecord;
use app\model\UserFish;
use app\model\UserFishRod;
use app\model\UserFriend;
use app\model\UserIngredient;
use app\model\UserOrder;
use app\model\UserSign;
use app\model\UserSpaceTrain;
use app\model\UserStaff;
use app\model\UserStaffFr;
use app\model\UserTravel;
use app\model\UserWatchAdLog;
use app\model\UserWish;
use support\Request;
use support\Response;
use think\facade\Db;
use zjkal\TimeHelper;

class IndexController
{

    public function index(Request $request): Response
    {

        //返回N分钟的秒数(默认为1分钟)
        $int = TimeHelper::secondMinute(5);
        return json(['code' => 200, 'msg' => 'success', 'data' => $int]);
    }

    public function del_data(Request $request): Response
    {

        //清除数据库
        //用户表
        User::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);
        //用户星愿雕像表
        UserWish::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);
        //用户观看广告记录表
        UserWatchAdLog::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户气球旅行表
        UserTravel::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户角色碎片表
        UserStaffFr::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户角色表
        UserStaff::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户宇宙列车表
        UserSpaceTrain::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户签到表
        UserSign::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);


        //用户订单表
        UserOrder::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);


        //用户材料表
        UserIngredient::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);


        //用户好友关系表
        UserFriend::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户鱼竿表
        UserFishRod::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户鱼表
        UserFish::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户资产变更记录
        UserFinancialRecord::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户工厂槽位
        UserFactorySeat::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);
        //用户工厂表
        UserFactory::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户邮件
        UserEmail::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);

        //用户背包
        UserBackpack::destroy(function ($q) {
            $q->where('id', '>', 0);
        }, true);


        return json(['code' => 0, 'msg' => 'ok']);
    }


    //执行工厂
    public function tool_factory(): Response
    {
        $factory_list = (new \app\model\Factory())->select();

        foreach ($factory_list as $factory) {
            for ($i = 0; $i < $factory->max_lv; $i++) {
                $material = new Material();
                $material->factory_id = $factory->id;
                $material->cn_name = $factory->cn_name . "工具" . ($i + 1);
                $material->details = '这是' . $factory->cn_name . '生产出的工具';
                $material->save();
            }

        }
        return json(['code' => 0, 'msg' => 'ok']);
    }


    //游戏配置数据初始化
    public function init_game_config(): Response
    {
        foreach (GAME_CONFIG as $k => $v) {
            $game_config = new GameConfig();
            $game_config->key = $v;
            $game_config->value = 100;
            $game_config->remarks = $k;
            $game_config->save();
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }


    //材料
    public function tool_material(): Response
    {
        $material_list = (new Material())->select();

        foreach ($material_list as $material) {
            $str = $material->cn_name;
            preg_match_all('/\d+/', $str, $matches);
            $material->need_lv = $matches[0][0];
            $material->save();
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }



    //生成探索章节
    public function generate_explore_chapter(Request $request): Response
    {
        $explore_address_list = (new ExploreAddress())->select();
        //星球
        foreach ($explore_address_list as $explore_address) {
            for ($i = 0; $i < 4; $i++) {
                $explore_chapter = new ExploreChapter();
                $explore_chapter->explore_address_id = $explore_address->id;
                $explore_chapter->cn_name = "章节" . ($i + 1);
                $explore_chapter->details = '这是' . $explore_address->cn_name . '的第' . ($i + 1) . '章节';
                $explore_chapter->save();
            }
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    //生成探索故事
    public function generate_explore_story(Request $request): Response
    {
        $explore_chapter_list = (new ExploreChapter())->select();
        //章节
        foreach ($explore_chapter_list as $explore_chapter) {
            for ($i = 0; $i < 5; $i++) {
                $explore_story = new ExploreStory();
                $explore_story->explore_chapter_id = $explore_chapter->id;
                $explore_story->cn_name = "故事" . ($i + 1);
                $explore_story->details = '这是' . $explore_chapter->cn_name . '的第' . ($i + 1) . '故事';
                $explore_story->save();
            }
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    //生成探索任务
    public function generate_explore_task(Request $request): Response
    {
        $explore_story_list = (new ExploreStory())->select();
        //故事
        foreach ($explore_story_list as $explore_story) {
            for ($i = 0; $i < 3; $i++) {
                $explore_task = new ExploreTask();
                $explore_task->explore_story_id = $explore_story->id;
                $explore_task->cn_name = "任务" . ($i + 1);
                $explore_task->details = '这是' . $explore_story->cn_name . '的第' . ($i + 1) . '任务';
                $explore_task->save();
            }
        }

        return json(['code' => 0, 'msg' => 'ok']);
    }


    //生成探索任务需要提交的道具
    public function generate_explore_task_prop(Request $request): Response
    {
        $explore_task_list = (new ExploreTask())->select();

        //任务
        foreach ($explore_task_list as $explore_task) {

            //道具
            $props = [
                ['id' => 20001, 'num' => 1],
                ['id' => 20002, 'num' => 1],
            ];

            //任务所需要的工具
            $explore_task->props = json_encode($props);
            $explore_task->save();
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

    //生成探索故事完成后的对话
    public function generate_explore_story_dialogue(Request $request): Response
    {
        $explore_story_list = (new ExploreStory())->select();
        //章节 - 对话
        foreach ($explore_story_list as $explore_story) {
            for ($i = 0; $i < 5; $i++) {
                $explore_dialogue = new ExploreDialogue();
                $explore_dialogue->explore_story_id = $explore_story->id;
                //对话
                $explore_dialogue->dialogue = json_encode([
                    ['type' => 1, 'content' => "恭喜你完成了" . $explore_story->cn_name],
                    ['type' => 2, 'content' => "谢谢"],
                ]);
                $explore_dialogue->save();
            }
        }
        return json(['code' => 0, 'msg' => 'ok']);

    }


    //生成探索导航
    public function generate_explore_navigation(Request $request): Response
    {

        //生成导航
        $explore_address_list = (new ExploreAddress())->select();

        $type = [//类型 1=角色 2=服装 3=建筑 4=卡牌 5=鱼类
            '角色' => 1,
            '服装' => 2,
            '建筑' => 3,
            '卡牌' => 4,
            '鱼类' => 5,
        ];

        foreach ($explore_address_list as $explore_address) {
            //普松星球
            //角色收集：黑巧曲奇，脆脆薄饼，马卡龙，普松，香脆星星，勇气星星
            //卡牌收集：黑卡1，金卡3，银卡6
            //服装收集：华丽1，精致2，普通3
            //鱼类收集：难逢2，寻常5

            if ($explore_address->cn_name == '普松星球') {
                $role_name = ['黑巧曲奇', '脆脆薄饼', '马卡龙', '普松', '香脆星星', '勇气星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());

                $card_name = ['黑卡1', '金卡3', '银卡6'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());


                $cos_name = ['华丽服装1', '精致服装2', '普通服装3'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());

                $fish_name = ['鲤鱼', '鲫鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }
            //绵花雷电松星球
            //角色收集：棉花雷电羊松，闪电薯条，摇滚可乐，激情爆米花，跳跳糖，动感星星，闪耀星星
            //卡牌收集：黑金1，黑卡2，金卡3，银卡6
            //服装收集：尊贵1，华丽1，精致2，普通3
            //鱼类收集：稀世1，难逢1，寻常4
            if ($explore_address->cn_name == '绵花雷电松星球') {
                $role_name = ['棉花雷电羊松', '闪电薯条', '摇滚可乐', '激情爆米花', '跳跳糖', '动感星星', '闪耀星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());
                $card_name = ['黑金1', '黑卡2', '金卡3', '银卡6'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());
                $cos_name = ['尊贵服装1', '华丽服装1', '精致服装2', '普通服装3'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());
                $fish_name = ['鲤鱼', '鲫鱼', '草鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }

            //水瓶松星球
            //角色收集：水瓶松，牛奶，美式咖啡，生椰拿铁，珍珠奶茶，丝滑星星，启明星星
            //卡牌收集：钻卡1，黑金1，黑卡2，金卡3，银卡4
            //服装收集：典藏1，尊贵1，华丽1，精致2，普通2
            //鱼类收集：稀世2，难逢3，寻常5
            if ($explore_address->cn_name == '水瓶松星球') {
                $role_name = ['水瓶松', '牛奶', '美式咖啡', '生椰拿铁', '珍珠奶茶', '丝滑星星', '启明星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());
                $card_name = ['钻卡1', '黑金1', '黑卡2', '金卡3', '银卡4'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());
                $cos_name = ['典藏服装1', '尊贵服装1', '华丽服装1', '精致服装2', '普通服装2'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());
                $fish_name = ['鲫鱼', '草鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }

            //人鱼松星球
            //角色收集：人鱼松，果冻，章鱼烧，冰糖，思念星星，软糯星星
            //卡牌收集：黑金1，黑卡2，金卡3，银卡5
            //服装收集：尊贵1，精致2，普通3
            //鱼类收集：稀世1，难逢2，寻常4
            if ($explore_address->cn_name == '人鱼松星球') {
                $role_name = ['人鱼松', '果冻', '章鱼烧', '冰糖', '思念星星', '软糯星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());
                $card_name = ['黑金1', '黑卡2', '金卡3', '银卡5'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());
                $cos_name = ['尊贵服装1', '精致服装2', '普通服装3'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());
                $fish_name = ['鲤鱼', '鲫鱼', '草鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }

            //小丑松星球
            //角色收集：小丑松，棉花糖，小熊软糖，薄荷糖，甜甜圈，甜蜜星星，虚空星星
            //卡牌收集：黑金1，黑卡2，金卡3，银卡5
            //装饰建筑收集：待定
            //服装收集：华丽1，精致2，普通3
            //鱼类收集：稀世1，难逢2，寻常4
            if ($explore_address->cn_name == '小丑松星球') {
                $role_name = ['小丑松', '棉花糖', '小熊软糖', '薄荷糖', '甜甜圈', '甜蜜星星', '虚空星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());
                $card_name = ['黑金1', '黑卡2', '金卡3', '银卡5'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());
                $cos_name = ['华丽服装1', '精致服装2', '普通服装3'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());
                $fish_name = ['鲤鱼', '鲫鱼', '草鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }

            //占星松星球
            //角色收集：占星松，桂花糕，熊猫汤圆，柿饼，竹笋，巨大星星，宇宙星星
            //卡牌收集：钻卡1，黑金1，黑卡2，金卡3，银卡5
            //装饰建筑收集：
            //服装收集：典藏1，华丽1，精致2，普通3
            //鱼类收集：稀世2，难逢3，寻常5
            if ($explore_address->cn_name == '占星松星球') {
                $role_name = ['占星松', '桂花糕', '熊猫汤圆', '柿饼', '竹笋', '巨大星星', '宇宙星星'];
                $this->extracted($role_name, $explore_address, $type['角色'], new Staff());
                $card_name = ['钻卡1', '黑金1', '黑卡2', '金卡3', '银卡5'];
                $this->extracted($card_name, $explore_address, $type['卡牌'], new Card());
                $cos_name = ['典藏服装1', '华丽服装1', '精致服装2', '普通服装3'];
                $this->extracted($cos_name, $explore_address, $type['服装'], new Cos());
                $fish_name = ['鲤鱼', '鲫鱼', '草鱼'];
                $this->extracted($fish_name, $explore_address, $type['鱼类'], new Fish());
            }

        }

        return json(['code' => 200, 'msg' => '成功']);


    }


    public function extracted(array $list_name, $explore_address, $type, $model)
    {
        foreach ($list_name as $item) {
            $pid = $model->where('cn_name', $item)->value('id');
            $is_have = (new ExploreNavigation())->where('explore_address_id', $explore_address->id)->where('pid', $pid)->where('type', $type)->find();
            if ($is_have) {
                continue;
            }

            $explore_navigation = new ExploreNavigation();
            $explore_navigation->explore_address_id = $explore_address->id;
            $explore_navigation->type = $type;
            $explore_navigation->pid = $pid;
            $explore_navigation->save();
        }
    }

}
